package main

import (
	"flag"

	v1 "helloworld/api/server/v1"

	"gitee.com/carlzyhuang/framework/app"
	"gitee.com/carlzyhuang/framework/log"
	"gitee.com/carlzyhuang/framework/rpc"
	_ "gitee.com/carlzyhuang/framework/storage"
	_ "go.uber.org/automaxprocs"

	_ "gitee.com/carlzyhuang/framework/storage/mysql"
	_ "gitee.com/carlzyhuang/framework/storage/redis"

	_ "gitee.com/carlzyhuang/framework/naming/etcd"

	"helloworld/internal/app/service"
	"helloworld/internal/domain"
	"helloworld/internal/infras"
)

var (
	// flagconf is the config flag.
	flagconf string
	// Version is the version of the compiled software.
	Version string
)

func init() {
	flag.StringVar(&flagconf, "conf", "../../configs", "config path, eg: -conf configs/config.yaml")
	rpc.Version = Version
}

func initRpcModule() {
	d := &infras.Data{}
	da := infras.NewGreeterRepo(d)
	uc := domain.NewGreeterUsecase(da)
	greeter := service.NewGreeterService(uc)
	wsGreeter := service.NewWsGreeterService(uc)

	v1.RegisterGreeterServer(rpc.DefaulRpcModule().GrpcServer(), greeter)
	v1.RegisterGreeterHTTPServer(rpc.DefaulRpcModule().HttpServer(), greeter)

	/// eg: 注册多个service到同一个server
	tester := service.NewTestService(uc)
	v1.RegisterTestServer(rpc.DefaulRpcModule().GrpcServer(), tester)
	v1.RegisterTestHTTPServer(rpc.DefaulRpcModule().HttpServer(), tester)

	// eg: 注册websocket service到对应的server
	// tip： 绑定实现了 WsGreeterWsServer 接口的服务到 WsServer
	v1.RegisterWsGreeterWsServer(rpc.DefaulRpcModule().WsServer(), wsGreeter)
}

func main() {
	flag.Parse()

	if err := app.Load(flagconf); err != nil {
		log.Anyf("load config: %s", err)
		return
	}

	initRpcModule()

	if err := app.Run(); err != nil {
		log.Anyf("app run: %s", err)
	}

}
